{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 相似图片搜索API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.80e378a905998e0e2e479058353b2c09.315360000.1910963158.282335-21219394', 'expires_in': 2592000, 'session_key': '9mzdDZAI3WghY5rkyunGor6Z8uHzmDo6gbMe3l78vCaLxR/vl7ohb3+vkE1Uqe+egHgy2koBQWdbEthlmYRgMQpsg8uyDg==', 'access_token': '24.6f1042699088d6aa00f31c77142e52d9.2592000.1598195158.282335-21219394', 'scope': 'public brain_all_scope vis-classify_实时检索-相似 brain_realtime_same_hq brain_realtime_similar brain_realtime_product wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': 'a896cf9d480c7f085acf6b50f371ca75'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "import pandas as pd \n",
    "import requests\n",
    "import base64\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=30Z2tiRTPLdpgzRemdXHGvMT&client_secret=SbdcGOIxPuXkuFVyUhw00yFs3dH0Xqy0'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图片入库——搭建宠物信息库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 1051674576673565938, 'error_code': 216681, 'brief': '{\"品种\":\"中华田园猫\", \"颜色\":\"黑白\",\"特征\":\"白脸，脑袋上黑色部分想中分刘海\",\"重量\":\"10kg\",\"身长\":\"20cm\",\"年龄\":\"3岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"荔湾区\\\\, \"街道\\\\镇\":\"西湾路\"}', 'cont_sign': '2678031493,2693992034', 'error_msg': 'item has existed'}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "相似图检索—入库\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\图\\中分-1.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"brief\":\"{\\\"品种\\\":\\\"中华田园猫\\\", \\\"颜色\\\":\\\"黑白\\\",\\\"特征\\\":\\\"白脸，脑袋上黑色部分想中分刘海\\\",\\\"重量\\\":\\\"10kg\\\",\\\"身长\\\":\\\"20cm\\\",\\\"年龄\\\":\\\"3岁\\\",\\\"省份\\\":\\\"广东\\, \\\"城市\\\":\\\"广州\\\", \\\"区/县\\\":\\\"荔湾区\\, \\\"街道\\镇\\\":\\\"西湾路\\\"}\",\n",
    "          \"image\":img}\n",
    "access_token = '24.89ae829bb930d00d914ddd5ff5ebe6dd.2592000.1596884846.282335-21219394'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 7935232897846495986, 'error_code': 216681, 'brief': '{\"品种\":\"英短银渐层\", \"颜色\":\"银白\",\"特征\":\"有一点灰毛\",\"重量\":\"10kg\",\"身长\":\"20cm\",\"年龄\":\"3岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"从化区\\\\, \"街道\\\\镇\":\"良口镇\"}', 'cont_sign': '1219883489,4235342367', 'error_msg': 'item has existed'}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "相似图检索—入库\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\图\\李白-2.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"brief\":\"{\\\"品种\\\":\\\"英短银渐层\\\", \\\"颜色\\\":\\\"银白\\\",\\\"特征\\\":\\\"有一点灰毛\\\",\\\"重量\\\":\\\"10kg\\\",\\\"身长\\\":\\\"20cm\\\",\\\"年龄\\\":\\\"3岁\\\",\\\"省份\\\":\\\"广东\\, \\\"城市\\\":\\\"广州\\\", \\\"区/县\\\":\\\"从化区\\, \\\"街道\\镇\\\":\\\"良口镇\\\"}\",\n",
    "          \"image\":img}\n",
    "access_token = '24.89ae829bb930d00d914ddd5ff5ebe6dd.2592000.1596884846.282335-21219394'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 4973951489263777778, 'error_code': 216681, 'brief': '{\"品种\":\"曼基康短腿猫\", \"颜色\":\"橘黄\",\"特征\":\"短腿\",\"重量\":\"11kg\",\"身长\":\"15cm\",\"年龄\":\"2岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"从化区\\\\, \"街道\\\\镇\":\"街口街道\"}', 'cont_sign': '3897504419,1047382538', 'error_msg': 'item has existed'}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "相似图检索—入库\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\图\\猪皮-1.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"brief\":\"{\\\"品种\\\":\\\"曼基康短腿猫\\\", \\\"颜色\\\":\\\"橘黄\\\",\\\"特征\\\":\\\"短腿\\\",\\\"重量\\\":\\\"11kg\\\",\\\"身长\\\":\\\"15cm\\\",\\\"年龄\\\":\\\"2岁\\\",\\\"省份\\\":\\\"广东\\, \\\"城市\\\":\\\"广州\\\", \\\"区/县\\\":\\\"从化区\\, \\\"街道\\镇\\\":\\\"街口街道\\\"}\",\n",
    "          \"image\":img}\n",
    "access_token = '24.89ae829bb930d00d914ddd5ff5ebe6dd.2592000.1596884846.282335-21219394'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查找相似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'has_more': False, 'log_id': 8156949477925869618, 'result_num': 3, 'result': [{'brief': '{\"品种\":\"英短银渐层\", \"颜色\":\"银白\",\"特征\":\"有一点灰毛\",\"重量\":\"10kg\",\"身长\":\"20cm\",\"年龄\":\"3岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"从化区\\\\, \"街道\\\\镇\":\"良口镇\"}', 'score': 0.77564400434494, 'cont_sign': '1219883489,4235342367'}, {'brief': '{\"品种\":\"曼基康短腿猫\", \"颜色\":\"橘黄\",\"特征\":\"短腿\",\"重量\":\"11kg\",\"身长\":\"15cm\",\"年龄\":\"2岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"从化区\\\\, \"街道\\\\镇\":\"街口街道\"}', 'score': 0.72551387548447, 'cont_sign': '3897504419,1047382538'}, {'brief': '{\"品种\":\"中华田园猫\", \"颜色\":\"黑白\",\"特征\":\"白脸，脑袋上黑色部分想中分刘海\",\"重量\":\"10kg\",\"身长\":\"20cm\",\"年龄\":\"3岁\",\"省份\":\"广东\\\\, \"城市\":\"广州\", \"区/县\":\"荔湾区\\\\, \"街道\\\\镇\":\"西湾路\"}', 'score': 0.69140356779099, 'cont_sign': '2678031493,2693992034'}]}\n"
     ]
    }
   ],
   "source": [
    "## \n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\图\\李白-1.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\n",
    "         \n",
    "         \"tag_logic\":\"1\"}\n",
    "\n",
    "access_token = '24.89ae829bb930d00d914ddd5ff5ebe6dd.2592000.1596884846.282335-21219394'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>brief</th>\n",
       "      <th>score</th>\n",
       "      <th>cont_sign</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{\"品种\":\"英短银渐层\", \"颜色\":\"银白\",\"特征\":\"有一点灰毛\",\"重量\":\"10...</td>\n",
       "      <td>0.775644</td>\n",
       "      <td>1219883489,4235342367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{\"品种\":\"曼基康短腿猫\", \"颜色\":\"橘黄\",\"特征\":\"短腿\",\"重量\":\"11kg...</td>\n",
       "      <td>0.725514</td>\n",
       "      <td>3897504419,1047382538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{\"品种\":\"中华田园猫\", \"颜色\":\"黑白\",\"特征\":\"白脸，脑袋上黑色部分想中分刘海...</td>\n",
       "      <td>0.691404</td>\n",
       "      <td>2678031493,2693992034</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               brief     score  \\\n",
       "0  {\"品种\":\"英短银渐层\", \"颜色\":\"银白\",\"特征\":\"有一点灰毛\",\"重量\":\"10...  0.775644   \n",
       "1  {\"品种\":\"曼基康短腿猫\", \"颜色\":\"橘黄\",\"特征\":\"短腿\",\"重量\":\"11kg...  0.725514   \n",
       "2  {\"品种\":\"中华田园猫\", \"颜色\":\"黑白\",\"特征\":\"白脸，脑袋上黑色部分想中分刘海...  0.691404   \n",
       "\n",
       "               cont_sign  \n",
       "0  1219883489,4235342367  \n",
       "1  3897504419,1047382538  \n",
       "2  2678031493,2693992034  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = response.json()\n",
    "results_data = results['result']  \n",
    "df = pd.DataFrame(results_data)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除图库照片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 8227795800391675986}\n"
     ]
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/delete\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\图\\李白-1.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.89ae829bb930d00d914ddd5ff5ebe6dd.2592000.1596884846.282335-21219394'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 动物识别API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.82648cea278678d285d0393af5f31fca.315360000.1910963394.282335-21582544', 'expires_in': 2592000, 'session_key': '9mzdA5rgMUv8c5fWapU+/F/sP/c7ljducmff07mEfwnU5l623kOxdlO645L7JRvxEfiKIMJs7pRps0xNsvtIu2z5GfwqFw==', 'access_token': '24.804a244c39300323b40df5a4e4ed0b3d.2592000.1598195394.282335-21582544', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': '63f279a494e25e05faa15abb9efcc926'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "import pandas as pd \n",
    "import requests\n",
    "import base64\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xGzCQrllmNVChYShcCNW6icd&client_secret=vGrEHraFAlgii2uKfZzS9ipOekIKohtL'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 4172750746406035544, 'result': [{'score': '0.233858', 'name': '波米拉猫'}, {'score': '0.182031', 'name': '英国短毛猫'}, {'score': '0.0961306', 'name': '巴西短毛猫'}, {'score': '0.0751026', 'name': '金吉拉猫'}, {'score': '0.046777', 'name': '英国长毛猫'}, {'score': '0.0363649', 'name': '褴褛猫'}]}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\諳\\Desktop\\李白-2.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.804a244c39300323b40df5a4e4ed0b3d.2592000.1598195394.282335-21582544'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
